package com.tag;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PaginationTag extends TagSupport {

    private final Logger logger = LoggerFactory.getLogger(PaginationTag.class);

    private int pageIndex;
    private int pageSize = 10;
    private int count;
    private String numberBtnSwitch = "on";
    private int numberBtn = 3;
    private int steps = 10;
    private String pageSizeString = "10,15,20,25,50";

    @Override
    public int doStartTag() throws JspException {
        try {
            JspWriter writer = pageContext.getOut();
            writer.write(createPagingHtm());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return SKIP_BODY;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getNumberBtnSwitch() {
        return numberBtnSwitch;
    }

    public void setNumberBtnSwitch(String numberBtnSwitch) {
        this.numberBtnSwitch = numberBtnSwitch;
    }

    public int getNumberBtn() {
        return numberBtn;
    }

    public void setNumberBtn(int numberBtn) {
        this.numberBtn = numberBtn;
    }

    public int getSteps() {
        return steps;
    }

    public void setSteps(int steps) {
        this.steps = steps;
    }

    public String getPageSizeString() {
        return pageSizeString;
    }

    public void setPageSizeString(String pageSizeString) {
        this.pageSizeString = pageSizeString;
    }

    public List<Integer> getPageSizeList() {
        String regex = "\\d+(,\\d+)*";
        if (!pageSizeString.matches(regex)) {
            pageSizeString = "10,15,20,25,50";
        }
        String[] pageSizeArray = pageSizeString.split(",");
        List<Integer> pageSizeString = new ArrayList<Integer>();
        for (String pageSize : pageSizeArray) {
            pageSizeString.add(Integer.valueOf(pageSize));
        }
        return pageSizeString;
    }

    public int getPrevious() {
        return pageIndex - 1 > 0 ? pageIndex - 1 : 1;
    }

    public int getNext() {
        return pageIndex + 1 <= getLastPage() ? pageIndex + 1 : getLastPage();
    }

    public int getLastPage() {
        return ((count - 1) / pageSize) + 1;
    }

    private String createPagingHtm() {
        return createLeftHtm() + createRightHtm();
    }


    private String createLeftHtm() {
        StringBuilder sb0 = new StringBuilder("<div class=\"select-container select select-container50");
        sb0.append(" input-group-btn col-dm-3 number left width-pageSize\">");
        sb0.append("<select class=\"select select-offscreen number-select paging-size-List\">");
        List<Integer> pageSizeList = getPageSizeList();
        for (Integer pageSize : pageSizeList) {
            sb0.append("<option value=\"").append(pageSize).append("\"");
            if (pageSize.equals(this.pageSize)) {
                sb0.append(" selected=\"selected\"");
            }
            sb0.append(">").append(pageSize).append("</option>");
        }
        sb0.append("</select></div>");
        sb0.append("<div class=\"dataTables_info\" role=\"status\" aria-live=\"polite\">&nbsp;&nbsp;");
        if (count == 0) {
            sb0.append("显示第 0 到 0 条记录，共 0 条记录 0 / 0 页");
        } else {
            int start = (pageIndex - 1) * pageSize + 1;
            int end = (start - 1) + pageSize > count ? count : (start - 1) + pageSize;
            int maxPage = (count - 1) / pageSize + 1;
            sb0.append("显示第" + start + " 到 " + end + " 条记录，共 " + count + " 条记录 " + pageIndex + " / " + maxPage + " 页");
        }
        sb0.append("</div>");
        return sb0.toString();
    }

    private String createRightHtm() {
        StringBuilder sb0 = new StringBuilder("<div class=\"dataTables_paginate paging_full_numbers pagination-toolbar\">");
        sb0.append("<span class=\"dataTables_paginate-s\">");
        sb0.append("<a class=\"paginate_button first\" aria-controls=\"table2\" data-idx=\"1\">第一页</a>");
        sb0.append("<a class=\"paginate_button previous\" aria-controls=\"table2\" data-idx=\"").append(getPrevious()).append("\">上一页</a>");
        sb0.append("<span>");
        if (!"off".equals(numberBtnSwitch) && count > 0) {
            if (steps > numberBtn && pageIndex - steps > 0) {
                sb0.append("<a class=\"paginate_button\" aria-controls=\"table2\" data-idx=\"").append(pageIndex - steps).append("\" title=\"").append(pageIndex - steps).append("\">...</a>");
            }
            for (int i = numberBtn; i > 0; i--) {
                if (pageIndex - i > 0) {
                    sb0.append("<a class=\"paginate_button\" aria-controls=\"table2\" data-idx=\"").append(pageIndex - i).append("\">").append(pageIndex - i).append("</a>");
                }
            }
            sb0.append("<a class=\"paginate_button current\" aria-controls=\"table2\" data-idx=\"").append(pageIndex).append("\">").append(pageIndex).append("</a>");
            for (int i = 1; i <= numberBtn; i++) {
                if (pageIndex + i <= getLastPage()) {
                    sb0.append("<a class=\"paginate_button\" aria-controls=\"table2\" data-idx=\"").append(pageIndex + i).append("\">").append(pageIndex + i).append("</a>");
                }
            }
            if (steps > numberBtn && pageIndex + steps < getLastPage()) {
                sb0.append("<a class=\"paginate_button\" aria-controls=\"table2\" data-idx=\"").append(pageIndex + steps).append("\" title=\"").append(pageIndex + steps).append("\">...</a>");
            }
        }
        sb0.append("</span>");
        sb0.append("<a class=\"paginate_button next\" aria-controls=\"table2\" data-idx=\"").append(getNext()).append("\">下一页</a>");
        sb0.append("<a class=\"paginate_button last\" aria-controls=\"table2\" data-idx=\"").append(getLastPage()).append("\">最后一页</a>");
        sb0.append("</span></div>");
        return sb0.toString();
    }

}